package com.avast.personalbudgetapp.presenter.activity;

import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.avast.personalbudgetapp.R;
import com.avast.personalbudgetapp.global.constant.Constants;
import com.avast.personalbudgetapp.model.bean.Proto.Transaction;
import com.avast.personalbudgetapp.presenter.custom.CustomListViewAdapter;
import com.avast.personalbudgetapp.presenter.custom.CustomViewHolder;

public class TransactionListActivity extends AbstractActivity {
	
	public TransactionListActivity() {
		super(R.layout.fragment_transaction_list, R.menu.transaction_list);
	}
	
	protected void init(){		

		ArrayAdapter<Transaction> adapter = new CustomListViewAdapter(this, 
				R.layout.list_item, appService.getDirtyTransactions());
				
		ListView listView = new ListView(this);
		listView.setAdapter(adapter);
		
		OnItemClickListener clickListenerForUpdate = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				
				CustomViewHolder viewHolder = (CustomViewHolder) v.getTag();
				Intent intent = new Intent(getApplicationContext(), TransactionAddOrEditActivity.class);
				intent.putExtra(Constants.INTENT_TRANSACTION_EDIT_MODE, Constants.TRANSACTION_UPDATE);
				intent.putExtra(Constants.INTENT_TRANSACTION_GUID, viewHolder.getGuid());
				intent.putExtra(Constants.INTENT_TRANSACTION_KIND, viewHolder.getTxtKind().getText());
				intent.putExtra(Constants.INTENT_TRANSACTION_VALUE, viewHolder.getTxtValue().getText());
				intent.putExtra(Constants.INTENT_TRANSACTION_DATE, viewHolder.getTxtDate().getText());
				startActivity(intent);
				
			}
		};
		
		listView.setOnItemClickListener(clickListenerForUpdate);		
		
		setContentView(listView);		
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_sync:
			appService.syncData();
//			goHome();
//			refresh();
			showTransactions(null);
			return true;
		case R.id.action_add:
			addTransaction();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
